var type = "";

function submitFn() {
    var param = checkParam();
    if ($.isPlainObject(param)) {
        if (type === "edit" || type === "add") {
            param.type = type;
            if (type === "edit") {
                param.id = getUrlParm("id");
            }
        } else {
            layer.msg("无法识别的操作！", fn);
            return false;
        }
        if ($(this).attr("data-val") == "saveAndPublish") {
            param.enable = 1;
        } else {
            param.enable = 0;
        }
        editFn(param);
    }
}

function editFn(param) {
    $.ajax({
        url: "/friendLink/back/editFriendLink.zb",
        type: "POST",
        dataType: "JSON",
        data: param
    }).done(function (res) {
        if (res && res.success) {
            if (param.type == "edit") {
                layer.msg(res.msg);
            } else {
                layer.msg("添加成功,即将刷新数据！", function () {
                    window.location.reload();
                })
            }
        } else {
            layer.msg(res.msg || "操作失败！", fn);
        }
    }).fail(function () {
        layer.msg("可能由于网络等原因操作失败！", fn);
    })
}

function checkParam() {
    var passTem = true;
    var param = {};
    param.site_name = $("#site_name").val().trim();
    param.home_url = $("#home_url").val().trim();
    param.favicon_url = $("#favicon_url").val().trim();
    for (var key in param) {
        if (!param[key]) {
            $("#" + key).attr("data-content", "该字段不能为空").popover('show');
            passTem = false;
        } else {
            $("#" + key).popover("hide");
        }
    }
    if (passTem) {
        return param;
    } else {
        return false;
    }
}

function loadDefault() {
    if (getUrlParm("type") == "edit") {
        type = "edit";
        $("title").text("更新友链");
        $.ajax({
            url: "/friendLink/back/getFriendLinkbyId.zb",
            type: "POST",
            dataType: "JSON",
            data: {id: getUrlParm("id")}
        }).done(function (res) {
            if (res) {
                $("#site_name").val(res.site_name);
                $("#home_url").val(res.home_url);
                $("#favicon_url").val(res.favicon_url);
                $("#descr").val(res.descr)
            }
        }).fail(function () {
            layer.msg("可能由于网络等原因加载公告失败！", fn)
        })
    } else {
        type = "add";
        $("title").text("添加友链");
    }
}

function getUrlParm(param) {
    var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function fn() {
}